package com.demo.web;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.demo.entity.MeUser;
import com.demo.support.Interceptor.JHandleInter;
import com.demo.support.util.OperateImage;
import com.demo.support.util.File.Field;
import com.demo.support.util.File.FileFormUtil;
import com.demo.support.util.File.FileTypeUtil;
import com.demo.web.base.BaseModel;
import com.jweb.annotaion.JController;
import com.jweb.annotaion.JValue;
import com.jweb.core.handle.Controller;
import com.jweb.util.UUIDUtil;
import com.jweb.util.WebUtil;
import com.jweb.util.validate.ValidateImg;

@JController("/public")
public class PublicController extends Controller {

	@JValue("user.filePath")
	public String userFilePath;

	public void getValidate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 设置响应的类型格式为图片格式
		response.setContentType("image/jpeg");
		// 禁止图像缓存。
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		ValidateImg vCode = new ValidateImg(200, 50, 4, 100);
		WebUtil.setSessionAttribute(request, "ValidateCode", vCode.getCode());
		vCode.write(response.getOutputStream());
	}

	/**
	 * 头像上传
	 * 
	 * @param request
	 * @param response
	 */
	public void fileUpload(HttpServletRequest request, HttpServletResponse response) {
		try {
			BaseModel base = new BaseModel(1, "OK");
			FileFormUtil ffu = new FileFormUtil();
			Iterator<Field> iterator = ffu.analysis(request);
			while (iterator.hasNext()) {
				Field f = iterator.next();
				InputStream input = new ByteArrayInputStream(f.getFieldData());
				// ======== 缓冲输入流 ========
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				byte[] buffer = new byte[1024];
				int len;
				while ((len = input.read(buffer)) > -1)
					baos.write(buffer, 0, len);
				baos.flush();
				// ========== 缓冲读取输入流,判断类型 ===========
				InputStream stream1 = new ByteArrayInputStream(baos.toByteArray());
				String fileType = FileTypeUtil.getFileByFile(stream1);
				// ========== 缓冲读取输入流,转换压缩 ============
				InputStream stream2 = new ByteArrayInputStream(baos.toByteArray());
				OperateImage oi = new OperateImage();
				InputStream is = oi.zoomImage(stream2, 600, 0, fileType);
				MeUser user = (MeUser) request.getAttribute(JHandleInter.USER);
				String fileName = "TX" + user.getUserId() + "_" + UUIDUtil.getUUID(4, 62) + "." + fileType;
				String cdnPath = user.getUserId() + "/temp/";
				String dirPath = userFilePath + "/" + cdnPath;
				File path = new File(dirPath);
				if (!path.exists())
					path.mkdirs();
				String filePath = dirPath + fileName;
				ImageIO.write(ImageIO.read(is), fileType, new File(filePath));
				base.setObj(cdnPath + fileName);
				toWriterJSON(base);
			}
		} catch (Exception e) {
			e.printStackTrace();
			toWriterJSON(new BaseModel(0, "error"));
		}
	}
}
